//
//  ArtDetailViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-2.
//
//

#import "ArtDetailViewController.h"
#import "ArtBuyViewController.h"
#import "DeviceUtil.h"
#import "RTLabel.h"

@interface ArtDetailViewController ()

@end

@implementation ArtDetailViewController
{
    UIButton *_sitebtn;
    UIButton *_recommbtn;
    UIButton *_traficbtn;
    
    UIScrollView *_myscrollview;
    RTLabel *_desclbl;
}

- (void)dealloc
{
    [_myscrollview release];
    [_desclbl release];
    
    _myscrollview = nil;
    _desclbl = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//实始化视图
- (void)init_view
{
    self.title = @"演艺票";
    //加载背影
     [self.view setBackgroundColor:BackgroundColor];
    
    //加载标题
    UILabel *titlelbl = [[[UILabel alloc]initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+70, 300, 40)]autorelease];
    titlelbl.text = self.artInfo.ProjectName;
    titlelbl.font = [UIFont systemFontOfSize:20];
    titlelbl.textColor = [UIColor redColor];
    titlelbl.backgroundColor = [UIColor clearColor];
    titlelbl.adjustsFontSizeToFitWidth = YES;
    titlelbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titlelbl];
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+120, 100, 150)]autorelease];
    NSURL *photourl = [NSURL URLWithString:self.artInfo.ImageUrl];
    //url请求实在UI主线程中进行的
    UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
    imgview.image = images;
    [self.view addSubview:imgview];
    
    //所在地
    UILabel *locationlbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+110, 190, 30)]autorelease];
    locationlbl.text = self.artInfo.Address;
    locationlbl.font = [UIFont systemFontOfSize:15];
    locationlbl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:locationlbl];
    
    //场馆
    UILabel *gemlbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+140, 190, 30)]autorelease];
    gemlbl.text = self.artInfo.FieldName;
    gemlbl.font = [UIFont systemFontOfSize:15];
    gemlbl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:gemlbl];
    
    //时间
    UILabel *timelbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+170, 190, 30)]autorelease];
    timelbl.text = self.artInfo.PerformTime;
    timelbl.font = [UIFont systemFontOfSize:15];
    timelbl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:timelbl];
    
    //现价
    int maxprice = 99999999;
    int minprice = 0;
    for (int i = 0; i < [self.artInfo.priceInfoArray count]; i++) {
        PriceInfo *priceInfo = [self.artInfo.priceInfoArray objectAtIndex:i];
        int priceValue =  [priceInfo.Price intValue];
        if (maxprice > priceValue) {
            maxprice = priceValue;
        }
        if (minprice < priceValue) {
            minprice = priceValue;
        }
    }
    
    UILabel *nowpricelbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+200, 190, 30)]autorelease];
    nowpricelbl.text = [NSString stringWithFormat:@"￥%d-%d", maxprice, minprice];
    nowpricelbl.font = [UIFont systemFontOfSize:15];
    nowpricelbl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:nowpricelbl];

    //电话咨询
//    UIButton *ballbtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    ballbtn.frame = CGRectMake(40, 190, 100, 30);
//    [ballbtn setBackgroundImage:[UIImage imageNamed:@"lanpai1"] forState:UIControlStateNormal];
//    [ballbtn setTitle:@"电话咨询" forState:UIControlStateNormal];
//    [self.view addSubview:ballbtn];
//    [ballbtn addTarget:self action:@selector(callEvent) forControlEvents:UIControlEventTouchUpInside];
    
    //支付
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+240, 100, 30);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.view addSubview:nextStep];
    [nextStep addTarget:self action:@selector(payEvent) forControlEvents:UIControlEventTouchUpInside];
    
    //详细信息
    [self initDetail];
}

//详细信息
- (void)initDetail
{
//    NSInteger width = [DeviceUtil getScreenWidth];
//    //景点介绍
//    _sitebtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    _sitebtn.frame = CGRectMake(0, 230, width/3, 30);
//    _sitebtn.backgroundColor = [UIColor grayColor];
//    [_sitebtn setTitle:@"票务介绍" forState:UIControlStateNormal];
//    [self.view addSubview:_sitebtn];
//    [_sitebtn addTarget:self action:@selector(siteEvent) forControlEvents:UIControlEventTouchUpInside];
//    
//    //经理推荐
//    _recommbtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    _recommbtn.frame = CGRectMake(width/3, 230, width/3, 30);
//    _recommbtn.backgroundColor = [UIColor lightGrayColor];
//    [_recommbtn setTitle:@"经理推荐" forState:UIControlStateNormal];
//    [self.view addSubview:_recommbtn];
//    [_recommbtn addTarget:self action:@selector(recommEvent) forControlEvents:UIControlEventTouchUpInside];
//    
//    //交通
//    _traficbtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    _traficbtn.frame = CGRectMake(width/3*2, 230, width/3, 30);
//    _traficbtn.backgroundColor = [UIColor lightGrayColor];
//    [_traficbtn setTitle:@"交通" forState:UIControlStateNormal];
//    [self.view addSubview:_traficbtn];
//    [_traficbtn addTarget:self action:@selector(traficEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [self initDesc:self.artInfo.summary];
}

//实始化描述
- (void)initDesc:(NSString*)tempstr
{
    int length = [self calc_charsetNum:tempstr]/25 + 1;
    
    int len = IOS7_OR_MORE?0.0:120;
    _myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+290, 300, 90+len)];
    _myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    _myscrollview.pagingEnabled = NO; //是否翻页
    _myscrollview.backgroundColor = [UIColor whiteColor];
    _myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    _myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    _myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    _myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(self.view.frame.size.width, length*15);
    [_myscrollview setContentSize:newSize];
    
    //加载电影描述
    NSInteger width = [DeviceUtil getScreenWidth];
    _desclbl = [[RTLabel alloc]initWithFrame:CGRectMake(10, 0, width-10, length*15)];
    _desclbl.text = tempstr;
    _desclbl.font = [UIFont systemFontOfSize:12];
    _desclbl.backgroundColor = [UIColor whiteColor];
    
    [_myscrollview addSubview:_desclbl];
    [self.view addSubview:_myscrollview];
}

//更新视图展示
- (void)renewScrollView:(NSString*)tempstr
{
    if (_desclbl) {
        [_desclbl removeFromSuperview];
        [_desclbl release];
        _desclbl = nil;
        
        int length = [self calc_charsetNum:tempstr]/25 + 1;
        NSInteger width = [DeviceUtil getScreenWidth];
        _desclbl = [[RTLabel alloc]initWithFrame:CGRectMake(10, 0, width-10, length*15)];
        _desclbl.text = tempstr;
        _desclbl.font = [UIFont systemFontOfSize:12];
        _desclbl.backgroundColor = [UIColor whiteColor];
        
        [_myscrollview addSubview:_desclbl];
    }
}

//电话咨询
- (void)callEvent
{
    NSLog(@"call phone");
}

//发起支付
- (void)payEvent
{
    ArtBuyViewController *buyController = [[ArtBuyViewController alloc]init];
    buyController.artInfo = self.artInfo;
    [self.navigationController pushViewController:buyController animated:YES];
    [buyController release];
}

//景点介绍
- (void)siteEvent
{
    _sitebtn.backgroundColor = [UIColor grayColor];
    _recommbtn.backgroundColor = [UIColor lightGrayColor];
    _traficbtn.backgroundColor = [UIColor lightGrayColor];

    [self renewScrollView:self.artInfo.summary];
}

//经理推荐
- (void)recommEvent
{
    _sitebtn.backgroundColor = [UIColor lightGrayColor];
    _recommbtn.backgroundColor = [UIColor grayColor];
    _traficbtn.backgroundColor = [UIColor lightGrayColor];
    
    [self renewScrollView:self.artInfo.summary];
}

//交通
- (void)traficEvent
{
    _sitebtn.backgroundColor = [UIColor lightGrayColor];
    _recommbtn.backgroundColor = [UIColor lightGrayColor];
    _traficbtn.backgroundColor = [UIColor grayColor];
    
    [self renewScrollView:self.artInfo.summary];
}


//计算NSString长度
- (int)calc_charsetNum:(NSString*)_str
{
    unsigned result = 0;
    const char *tchar=[_str UTF8String];
    if (NULL == tchar) {
        return result;
    }
    
    
    for (int i=0; i<strlen(tchar);)
    {
        unsigned t = tchar;
        if( t >= 224 )
            i = i+3;
        else if( t >= 192 )
            i = i+2;
        else
            i++;
        result++;
    }
    
    return result;
    
}

@end





